package com.ruoyi.common.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;
import java.net.HttpCookie;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 四叶天代理
 */
@Slf4j
public class ProxiesUtil {

    public static String Proxies(String urlString, String ipPort) throws IOException {
        Response response = null;
        String ip = ipPort.split(":")[0];
        int port = Integer.parseInt(ipPort.split(":")[1]);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
        OkHttpClient httpClient = new OkHttpClient().newBuilder().proxy(proxy).connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = null;
        request = (new Request.Builder().url(urlString).build());
        response = httpClient.newCall(request).execute();
        return Objects.requireNonNull(response.body()).string();
    }

    public static String Proxies(String urlString, String ipPort, String cookie) {
        try {
            Response response = null;
            try {
                log.info("当前ip：" + ipPort);
                String ip = ipPort.split(":")[0];
                int port = Integer.parseInt(ipPort.split(":")[1]);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
                OkHttpClient httpClient = new OkHttpClient().newBuilder().proxy(proxy).connectTimeout(10, TimeUnit.SECONDS)
                        .build();
                Request request = null;
                request = (new Request.Builder().addHeader("Cookie", cookie).url(urlString).build());
                response = httpClient.newCall(request).execute();
                ResponseBody body = response.body();
                return body.string();
            } catch (IOException e) {
                log.info("错误提示1");
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.info("错误提示2");
            e.printStackTrace();
        }
        return null;
    }

    public static HttpResponse ProxiesHp(String urlString, String ipPort, List<HttpCookie> params) {
        String ip = ipPort.split(":")[0];
        int port = Integer.parseInt(ipPort.split(":")[1]);
        HttpRequest httpRequest = HttpRequest.get(urlString).timeout(30000);
        httpRequest.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)));
        httpRequest.cookie(params);
        return httpRequest.execute();
    }

    public static HttpResponse ProxiesPost(String urlString, String ipPort, Map<String, String> params) {
        String ip = ipPort.split(":")[0];
        int port = Integer.parseInt(ipPort.split(":")[1]);
        HttpRequest httpRequest = HttpRequest.post(urlString).timeout(30000);
        httpRequest.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)));
        if (Objects.nonNull(params)) {
            for (String s : params.keySet()) {
                httpRequest.header(s, params.get("s"));
            }
        }
        return httpRequest.execute();
    }

    public static HttpResponse ProxiesPost(String urlString, String ipPort, Map<String, String> head, String params) {
        String ip = ipPort.split(":")[0];
        int port = Integer.parseInt(ipPort.split(":")[1]);
        HttpRequest httpRequest = HttpRequest.post(urlString).timeout(30000);
        httpRequest.body(params);
        httpRequest.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)));
        if (Objects.nonNull(head)) {
            for (String s : head.keySet()) {
                httpRequest = httpRequest.header(s, head.get("s"));
            }
        }
        return httpRequest.execute();
    }


}
